<?php


namespace App\Http\Controllers\Car;


use App\Http\Controllers\Controller;
use App\Models\CarModels\GreenStaff;
use App\Models\SystemManagement\Department;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Validator;
/**
 * @group 运营管理->绿化运营->任务划分->区域人员配置
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class GreenStaffController extends Controller
{
    /**
     * 绿化区域人员查看列表
     *
     * @queryParam department string 对应人员组信息
     * @queryParam landscaping string 对应园林信息
     *
     * @bodyParam landscaping int optional 对应园林id
     * @bodyParam department int  optional 对应用户组id
     */
    public function index(Request $request)
    {
        $projectAreas = GreenStaff::where(function ($q) use($request){
                $request->has('landscaping') && $q->where('landscaping_id',$request->get('landscaping'));
                $request->has('department') && $q->where('department_id',$request->get('department'));
            })->with(['landscaping','department'])->get();
        return response()->json(encodeMsg($projectAreas,200));
    }
    /**
     * 绿化区域人员查看单个
     *
     * @queryParam department string 对应人员组信息
     * @queryParam landscaping string 对应园林信息
     *
     */
    public function show($id)
    {
        $projectAreas = GreenStaff::with(['landscaping','department'])->find($id);
        if($projectAreas == []) {
            return response()->json(encodeMsg(["message" => "没有该区域信息!"], 404));
        } else {
            return response()->json(encodeMsg($projectAreas,200));
        }
    }
    /**
     * 绿化区域人员添加
     *
     *
     * @bodyParam landscaping_id int required 对应园林信息 外键
     * @bodyParam department_id int required 对应人员组信息 外键
     *
     */
    public function store(Request $request)
    {
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json(encodeMsg($validator->errors()->messages(),400),400);
        }
        try{
            $projectAreas = GreenStaff::create($request->all());
            return response()->json(encodeMsg($projectAreas,200),200);
        }catch (QueryException $e){
            return response()->json(encodeMsg($e->getMessage(),500),500);
        }
    }
    /**
     * 绿化区域人员修改
     *
     *
     * @bodyParam landscaping_id int optional 对应园林信息 外键
     * @bodyParam department_id int optional 对应人员组信息 外键\
     *
     */
    public function update(Request $request, $id)
    {

        $validator = Validator::make($request->all(), genValidate());
        if ($validator->fails()) {
            return response()->json(encodeMsg($validator->errors()->messages(), 400), 400);
        }

        $carTask = GreenStaff::find($id);
        if ($carTask == []) {
            return response()->json(encodeMsg(["message" => "没有该任务信息!"], 404));
        }
        try{
            $carTask->update($request->all());
        }catch (QueryException $e) {
            return response()->json(encodeMsg(["message" => "修改失败"], 500),500);
        }
        return response()->json(encodeMsg($carTask, 200));

    }
    /**
     * 绿化区域人员删除
     *
     */
    public function destroy($pk){
        $data = GreenStaff::find($pk);
        if($data == []){
            return response()->json(encodeMsg(["message"=>"没有绿化区域信息"],404),404);
        }
        $data->delete();
        return response()->json(encodeMsg(["message"=>"删除成功"],204),200);
    }
}